# coding=utf-8
##
## Author: jmdvirus@aliyun.com
##
## Create: 2019年02月16日 星期六 11时29分11秒
##

import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn import model_selection as modsel
from sklearn import linear_model
import matplotlib.pyplot as plt

def load_data():
    boston = datasets.load_boston()
    #print("boston: ", boston)
    print("boston shape: ", boston.data.shape)
    return boston

def show(y_pred, y_test):
    plt.figure(figsize=(10,6))
    plt.plot(y_test, linewidth=3, label='ground truth')
    plt.plot(y_pred, linewidth=3, label='predicted')
    plt.legend(loc='best')
    plt.xlabel('test data points')
    plt.ylabel('target value')

def show2(linreg, x_test, y_pred, y_test):
    plt.plot(y_test, y_pred, 'o')
    plt.plot([-10, 60], [-10,60], 'k--')
    plt.axis([-10,60, -10, 60])
    plt.xlabel('ground truth')
    plt.ylabel('predicted')
    scorestr = r'R$^2$ = %.3f' % linreg.score(x_test, y_test)
    errstr = 'MSE = %0.3f' % metrics.mean_squared_error(y_test, y_pred)
    plt.text(-5, 50, scorestr, fontsize=12)
    plt.text(-5, 45, errstr, fontsize=12)

def train(data):
    linreg = linear_model.LinearRegression()
    ## 正则化 L1
    #linreg = linear_model.Lasso()
    ## 正则化 L2
    #linreg = linear_model.Ridge()
    x_train, x_test, y_train, y_test = modsel.train_test_split(
            data.data, data.target, test_size=0.1,
            random_state = 42)

    print("start to training...")
    linreg.fit(x_train, y_train)
    #LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1,
    #        normalize=False)
    mse = metrics.mean_squared_error(y_train, linreg.predict(x_train))
    print("mse: ", mse)
    score = linreg.score(x_train, y_train)
    print("score: ", score)

    print("on test model...")
    y_pred = linreg.predict(x_test)
    mse2 = metrics.mean_squared_error(y_test, y_pred)
    print("mse2: ", mse2)
    print("y_test: ", y_test)
    print("y_pred: ", y_pred)
    #show(y_pred, y_test)
    show2(linreg, x_test, y_pred, y_test)

if __name__ == "__main__":
    data = load_data()
    train(data)
    
    plt.show()

